﻿module Physics {
    /*
   this function uses the p2 collision calculations that are done on every step to find out if the player collides with something on the bottom - it returns true if a collision is happening
   */
    export function touchingVertical(someone: Phaser.Sprite, side?: string) {
        var game = someone.game;

        if (side == null)
            side = 'top';

            var yAxis = p2.vec2.fromValues(0, 1);
            var result = false;
            for (var i = 0; i < game.physics.p2.world.narrowphase.contactEquations.length; i++) {
                var c = game.physics.p2.world.narrowphase.contactEquations[i];  // cycles through all the contactEquations until it finds our "someone"
                if (c.bodyA === someone.body.data || c.bodyB === someone.body.data) {
                    var d = p2.vec2.dot(c.normalA, yAxis); // Normal dot Y-axis
                    if (c.bodyA === someone.body.data) d *= -1;
                    switch (side) {
                        case 'top':

                            if (d < -0.5) result = true;
                            break;
                        case 'bottom':
                            if (d > 0.5) result = true;
                            break;
                    }
                }
            } return result;
    }

    export function touchingSide(someone: Phaser.Sprite, side?: string) {
        var game = someone.game;

        if (side == null)
            side = 'both';


        var xAxis = p2.vec2.fromValues(1, 0);
        var result = false;
        for (var i = 0; i < game.physics.p2.world.narrowphase.contactEquations.length; i++) {
            var c = game.physics.p2.world.narrowphase.contactEquations[i];
            if (c.bodyA === someone.body.data || c.bodyB === someone.body.data) {
                var d = p2.vec2.dot(c.normalA, xAxis); // Normal dot X-axis
                if (c.bodyA === someone.body.data) d *= -1;
                switch (side) {
                    case 'left':
                        if (d < -0.5) result = true;
                        break;
                    case 'right':
                        if (d > 0.5) result = true;
                        break;
                    case 'both':
                        if (d > 0.5 || d < -0.5) result = true;
                        break;
                }
            }
        } return result;
    }

    export function distanceBetween(spriteA: Phaser.Sprite, spriteB: Phaser.Sprite) :number {
        var dx: number = spriteA.body.x - spriteB.body.x;
        var dy: number = spriteA.body.y - spriteB.body.y;

        var distance: number = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));

        return distance;
    }

    export function moveTo(spriteA: Phaser.Sprite, spriteB: Phaser.Sprite, speed:number) {
        var angle = Math.atan2(spriteB.body.y - spriteA.body.y, spriteB.body.x - spriteA.body.x);
        spriteA.body.velocity.x = Math.cos(angle) * speed;
    }
}